#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define A 2
#define B 4
#define C 5
#define D 9
char text[32] ="";

void AX()
{
    digitalWrite(A,HIGH);
    digitalWrite(B,LOW);
    digitalWrite(C,LOW);
    digitalWrite(D,LOW);
}
void BX()
{
    digitalWrite(A,LOW);
    digitalWrite(B,HIGH);
    digitalWrite(C,LOW);
    digitalWrite(D,LOW);
}
void CX()
{
    digitalWrite(A,LOW);
    digitalWrite(B,LOW);
    digitalWrite(C,HIGH);
    digitalWrite(D,LOW);
}
void DX()
{
    digitalWrite(A,LOW);
    digitalWrite(B,LOW);
    digitalWrite(C,LOW);
    digitalWrite(D,HIGH);
}
void tz()
{
    digitalWrite(A,LOW);
    digitalWrite(B,LOW);
    digitalWrite(C,LOW);
    digitalWrite(D,LOW);
}
void n_turn()
{
    DX();
    delay(5);
    CX();
    delay(5);
    BX();
    delay(5);
    AX();
    delay(5);
}

void s_turn()
{
    AX();
    delay(5);
    BX();
    delay(5);
    CX();
    delay(5);
    DX();
    delay(5);
}

RF24 radio(7, 8);
const byte address[6] = "00001";

void setup() {
//步进电机
pinMode(2,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(9,OUTPUT);
//rf24
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}

void loop() {
    while(1)
    {
        if(radio.available())
        {
            radio.read(&text, sizeof(text));
            if(text[0]=='D')
            {
            Serial.println(text);
            for(int i=0;i<=(4*64);i++)        //8次循环电机旋转一周，外轴减速比1：64，还需要*64
	        {
                s_turn();
            }
            delay(5);
            tz();
            text[32] ="";
            delay(5);
            }
            if(text[0]=='E')
            {
            Serial.println(text);
            for(int i=0;i<=(4*64);i++)        //8次循环电机旋转一周，外轴减速比1：64，还需要*64
	        {
                n_turn();
            }
            delay(5);
            tz();
            text[32] ="";
            delay(5);
            }
        }
    }
}